package com.woniuxy.controller;

import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.service.RegistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

/**
 * @Author 罗蔚
 * @Date 2022/4/5 17:25
 * @Description TODO
 */
@RestController
@RequestMapping("/registConreoller")
public class RegistController {
    @Autowired
    RegistService registService;

    @PostMapping("/existUsername")
    public ResponseEntity existUsername(@RequestBody @Valid User user) {
//        System.out.println(user.getUsername());
        return registService.existUsername(user.getUsername());
    }

    @PostMapping("/addUser")
    public ResponseEntity addUser(@RequestBody @Valid User user) {
        //System.out.println(user);
        return registService.addUser(user);
    }

    @RequestMapping("/active")
    public void active(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取激活码
        String code = request.getParameter("code");
        ResponseEntity responseEntity = registService.active(code);
        if(responseEntity.getCode() == 200){
            // 激活成功
            response.sendRedirect("/pages/active_ok.jsp");
        }else {
            // 激活失败
            response.sendRedirect("/pages/active_error.jsp");
        }
    }


}
